<html>
<head>
<script type="text/javascript">
var num_choices = new Array();
//var max_choice_limit = 0;
var max_choice_limit = <?php echo $count;?>;
function add_textbox($ind){
	//var index = document.getElementById("options").value;
	var myString = "";
	num_choices[$ind]++;
	tester = "";
	
	if (max_choice_limit == 0){
	}
	else{
		max_choice_limit++;
	}
	
	//alert(index);
	value = 0;
	for(i = 0; i < num_choices[$ind]; i++){
		//alert(i);
		value = i+1;
		search = "choice"+$ind+":"+value;
		//alert(search);
		if (i == num_choices[$ind]-1){
			tester = "";
		}
		else{
			tester = document.getElementById(search).value;
		}
		
		//alert(tester);
		myString += "Choice "+value+" <input id= 'choice"+$ind+":"+value+"' type='text' name='choice"+$ind+":"+value+"' value="+tester+">"+"<br>";
		//alert(myString);
	}
	document.getElementById("options"+$ind).innerHTML = myString;
	//document.getElementById("options").innerHTML += "Choice "+index+" <input type='text' name='choice"+index+"'>"+"<br>";
	
	document.getElementById("choices"+$ind).innerHTML = "<input type='hidden' name='choice_value"+$ind+"' value="+num_choices[$ind]+">";

	limit_string = "";
	for(i = 0; i < max_choice_limit; i++){
		value = i+1;
		limit_string += "<option value="+value+">"+value+"</option>";
	}
	if (max_choice_limit == 0){
		
	}
	else{
		//alert("wtf");
		//document.getElementById("limit").innerHTML = "";
		document.getElementById("limit").innerHTML = "<select name='choice_limit'>"+limit_string+"</select>";	
	}
	
}
function delete_textbox($ind){
	var myString = "";

	if (max_choice_limit == 0){
	}
	else{
		max_choice_limit--;
	}
	
	
	num_choices[$ind]--;
	if (num_choices[$ind] == 1){
		// must be atleast 2 choices
		num_choices[$ind]++;
		max_choice_limit++;
	}
	
	
	for(i = 0; i < num_choices[$ind]; i++){
		value = i+1;
		search = "choice"+$ind+":"+value;
		/*
		if (i == index-1){
			tester = "";
		}
		else{
			tester = document.getElementById(search).value;
		}*/
		tester = document.getElementById(search).value;
		//myString += "Choice "+value+" <input id='choice' type='text' name='choice"+value+"'>"+"<br>";
		myString += "Choice "+value+" <input id= 'choice"+$ind+":"+value+"' type='text' name='choice"+$ind+":"+value+"' value="+tester+">"+"<br>";
	}
	document.getElementById("options"+$ind).innerHTML = myString;
	//document.getElementById("options").innerHTML += "Choice "+index+" <input type='text' name='choice"+index+"'>"+"<br>";
	//index--;
	document.getElementById("choices"+$ind).innerHTML = "<input type='hidden' name='choice_value"+$ind+"' value="+num_choices[$ind]+">";			

	limit_string = "";
	for(i = 0; i < max_choice_limit; i++){
		value = i+1;
		limit_string += "<option value="+value+">"+value+"</option>";
	}
	if (max_choice_limit == 0){
		
	}
	else{
		//alert("wtf");
		//document.getElementById("limit").innerHTML = "";
		document.getElementById("limit").innerHTML = "<select name='choice_limit'>"+limit_string+"</select>";	
	}
	

}

function detect($index){
	var e = document.getElementById("selectedtype"+$index);
	var choice = e.options[e.selectedIndex].value;
	if (choice == "text"){
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "";
		document.getElementById("buttons"+$index).innerHTML = "";
		document.getElementById("del_buttons"+$index).innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "Required type <select name= 'required"+$index+"'>"
		+"<option value='1'>integer</option>"
		+"<option value='0'>text</option>"
		+"</select>";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='0'>";
	}
	else if (choice == "paragraph text"){
		//text area instead
		//alert(choice);
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "";
		document.getElementById("buttons"+$index).innerHTML = "";
		document.getElementById("del_buttons"+$index).innerHTML = "";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='0'>";
	}
	else if (choice == "multiple choice"){
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "Choice 1 <input id='choice"+$index+":1' type='text' name='choice"+$index+":1' >"+"<br>";
		document.getElementById("options"+$index).innerHTML += "Choice 2 <input id='choice"+$index+":2' type='text' name='choice"+$index+":2' > "+"<br>";
		document.getElementById("buttons"+$index).innerHTML = "<button type='button' onclick='add_textbox("+$index+")'>Add another </button>";
		document.getElementById("del_buttons"+$index).innerHTML = "<button type='button' onclick='delete_textbox("+$index+")'>Delete Choice </button>";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='2'>";
		num_choices[$index] = 2;
	}
	else if (choice == "checkboxes"){
		//check box
		//alert(choice);
		
		max_choice_limit = 2;
		//alert("a");
		limit_string = "";
		//check box
		//alert(choice);
		for(i = 0; i < max_choice_limit; i++){
			//document.write("a");
			value = i+1;
			limit_string += "<option value="+value+">"+value+"</option>";
		}
		//alert("a");
		document.getElementById("limit").innerHTML = "<select name='choice_limit'>"+limit_string+"</select>";
		
		
		document.getElementById("options"+$index).innerHTML = "Choice 1 <input id='choice"+$index+":1' type='text' name='choice"+$index+":1' >"+"<br>";
		document.getElementById("options"+$index).innerHTML += "Choice 2 <input id='choice"+$index+":2' type='text' name='choice"+$index+":2' > "+"<br>";
		document.getElementById("buttons"+$index).innerHTML = "<button type='button' onclick='add_textbox("+$index+")'>Add another </button>";
		document.getElementById("del_buttons"+$index).innerHTML = "<button type='button' onclick='delete_textbox("+$index+")'>Delete Choice </button>";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='2'>";
		num_choices[$index] = 2;
		//alert("a");
		//document.getElementById("options").innerHTML += "Gender <input type='checkbox' name='gender' value='male'>";
	}
	else if (choice == "choose"){
		//drop down
		//alert(choice);
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "Choice 1 <input id ='choice"+$index+":1' type='text' name='choice"+$index+":1' >"+"<br>";
		document.getElementById("options"+$index).innerHTML += "Choice 2 <input id='choice"+$index+":2' type='text' name='choice"+$index+":2' > "+"<br>";
		document.getElementById("buttons"+$index).innerHTML = "<button type='button' onclick='add_textbox("+$index+")'>Add another </button>";
		document.getElementById("del_buttons"+$index).innerHTML = "<button type='button' onclick='delete_textbox("+$index+")'>Delete Choice </button>";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='2'>";
		num_choices[$index] = 2;
	}
	else if (choice == "scale"){
		//agree disagree basically in essence
		//alert(choice);
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "Choice 1 <input type='text' name='choice"+$index+":"+"1' >"+"<br>";
		document.getElementById("options"+$index).innerHTML += "Choice 2 <input type='text' name='choice"+$index+":"+"2' > "+"<br>";
		document.getElementById("buttons"+$index).innerHTML = "";
		document.getElementById("del_buttons"+$index).innerHTML = "";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='2'>";
	}
	else if (choice == "ranking"){
		//ranking
		//alert(choice);
		document.getElementById("limit").innerHTML = "";
		document.getElementById("options"+$index).innerHTML = "Choice 1 <input id='choice"+$index+":1' type='text' name='choice"+$index+":1' >"+"<br>";
		document.getElementById("options"+$index).innerHTML += "Choice 2 <input id='choice"+$index+":2' type='text' name='choice"+$index+":2' > "+"<br>";
		document.getElementById("buttons"+$index).innerHTML = "<button type='button' onclick='add_textbox("+$index+")'>Add another </button>";
		document.getElementById("del_buttons"+$index).innerHTML = "<button type='button' onclick='delete_textbox("+$index+")'>Delete Choice </button>";
		document.getElementById("choices"+$index).innerHTML = "<input type='hidden' name='choice_value"+$index+"' value='2'>";
		num_choices[$index] = 2;
	}
	//document.write("<input type='hidden' name='len' value='6'>");
}
function selectObject(val)
{
	var e = document.getElementById('selectedtype');
	e.selectedIndex(val);
}

</script>
<title></title>
</head>
<body>
<?php 
echo form_open('user/actual_question_update/'.$survey_id.'/');

$N = count($questions);
echo "<input type='hidden' name='num_questions' value='".$N."'>";

for($i = 0; $i <$N; $i++)
{
	echo "Question:<br /><textarea id = 'question".$i."' name='question".$i."' cols='40' 
		rows='3'>".$questions[$i]->row(0)->body."</textarea><br/>";

	echo "Question type:<select name = 'type".$i."' id='selectedtype".$i."' onchange= 'detect(".$i.")'>";

	foreach($type as $value1)
	{
		foreach($value1 as $value)
		{
			if ($value == $questions[$i]->row(0)->type){
				echo "<option value =\"$value\" SELECTED>$value</option>";
			}
			else{
				echo "<option value=\"$value\">$value</option>";
			}
		}
		
	}
	echo "</select>";
	echo "<div id='required'></div>";
	if($questions[$i]->row(0)->type == 'text')
	{
		echo "<div id = 'options".$i."'>Required type <select name= 'required".$i."'>";
		if($questions[$i]->row(0)->required == 1)
		{
			echo "<option value='1' SELECTED>integer</option>
				<option value='0'>text</option>";
		}else{
			echo "<option value='1'>integer</option>
				<option value='0' SELECTED>text</option>";
		}
		echo "</select></div>";
		echo "<div id ='choices".$i."'><input type='hidden' name='choice_value".$i."' value='0'></div>";
		echo "<div id = 'buttons".$i."'></div>";
		echo "<div id = 'del_buttons".$i."'></div>";
	}
	else if($questions[$i]->row(0)->type == 'paragraph text')
	{
		echo "<div id = 'options".$i."'></div>";
		echo "<div id ='choices".$i."'><input type='hidden' name='choice_value".$i."' value='0'></div>";
		echo "<div id = 'buttons".$i."'></div>";
		echo "<div id = 'del_buttons".$i."'></div>";
	}
	else if(($questions[$i]->row(0)->type == 'multiple choice') || ($questions[$i]->row(0)->type == 'checkboxes') || 
				($questions[$i]->row(0)->type == 'ranking') || ($questions[$i]->row(0)->type == 'choose'))
	{
		$limit = $choices[$i]->num_rows;
		echo "<div id = 'options".$i."'>";
		for($j = 0; $j < $limit; $j++)
		{
			$k = $j+1;
			echo "Choice ". $k . "<input type='text' id='choice".$i.":".$k."' name = 'choice".$i.":".$k."' value = '". $choices[$i]->row($j)->body ."'><br>";
		}
		echo "</div>";		
		echo "<div id = 'buttons".$i."'><button type='button' onclick='add_textbox(".$i.")'>Add another </button></div>";
		echo "<div id = 'del_buttons".$i."'><button type='button' onclick='delete_textbox(".$i.")'>Delete Choice </button></div>";
		echo "<div id ='choices".$i."'><input type='hidden' name='choice_value".$i."' value='".$limit ."'></div>";
		?><script type = "text/javascript"> num_choices[<?php echo $i ?>] = <?php echo $limit ?> </script><?php
	}
	else if ($questions[$i]->row(0)->type == 'scale')
	{
		$limit = $choices[$i]->num_rows;
		echo "<div id = 'options".$i."'>";
		for($j = 0; $j < $limit; $j++)
		{
			$k = $j+1;
			echo "Choice ". $k . "<input type='text' name='choice".$i.":".$k."' value = '". $choices[$i]->row($j)->body ."'><br>";
		}
		echo "</div>";
		echo "<div id ='choices".$i."'><input type='hidden' name='choice_value' value='".$limit."'></div>";
		echo "<div id = 'buttons".$i."'></div>";
		echo "<div id = 'del_buttons".$i."'></div>";
	}
	//<div id ='options'></div>
	//<div id ='buttons'></div>
	//<div id ='del_buttons'></div>
	echo "<input type = 'checkbox' name='with_text".$i."' value='1'";
	if($questions[$i]->row(0)->with_text == 1)
	{
		echo "CHECKED";
	}
	echo "> With text<br/>";
	echo "Text label <input type = 'text' name='text_label".$i."' value='".$questions[$i]->row(0)->text_label."'><br><br>";
	echo "<input type='hidden' name='question_id".$i."' value='".$questions[$i]->row(0)->question_number."'>";
}
?>
<?php
	if($questions[0]->row(0)->null_field == 0){
		
		echo "<input type='checkbox' value='1' name='null'> ";
	}
	else{
		
		echo "<input type='checkbox' value='1' name='null' checked=\"true\"> ";
	}
	
?>
Make this a required question <br>

Possible answers<div id="limit">
<?php
if($questions[0]->row(0)->type == 'checkboxes'){
	//echo "Possible answers<br>";
	$len = $count;
	$limit = $questions[0]->row(0)->choice_limit;
	echo "<select name='choice_limit'>";
	for($i=1; $i<= $len; $i++){
		if ($i == $limit){
			echo "<option value=\"$i\" selected =\"selected\">";
			echo "$i";
			echo "</option>";			
		}
		else{
			echo "<option value=\"$i\">";
			echo "$i";
			echo "</option>";
		}
	}
	echo "</select>";
}
?>
</div>
<?php
	if ($has_groups){
		$category = $questions[0]->row(0)->category_name;
		echo "Belongs to";
		echo "<select name='category'>";
		foreach($categories->result() as $row){
			$cat = $row->category_name;
			if($row->category_name == $category){
				echo "<option value=\"$cat\" selected=\"selected\">";
				echo $row->category_name;
				echo "</option>";
			}
			else{
				echo "<option value=\"$cat\">";
				echo $row->category_name;
				echo "</option>";				
			}
		}
		echo "</select>";
	}
	
?>



<script type="text/javascript">
function getSelectedValue(){
	document.getElementById("selectedtype") //where "selectid" is the ID of the SELECT element on the page
	document.write(document.getElementById("selectedtype"));
}
</script>
<br/>
<div id ="choices"><input type="hidden" name='choice_value' value="0"></div>
<input type="submit" name="submit" value="Update"/>

<?php
	echo form_close();
?>


</body>
</html>